﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net.NetworkInformation;
using System.Linq;

public partial class Network_General : System.Web.UI.Page
{
    protected IPGlobalProperties IPGlobal { get; private set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        labHost.Text = Environment.MachineName;        
        switch (NetworkInfo.Instance.Status)
        {
            case JoinStatus.Unknown:
                labDomainWorkgroup.Text = "<unknown>";
                labMemberOf.Text = "<unknown>";
                break;
            case JoinStatus.UnJoined:
                labDomainWorkgroup.Text = "<unjoined>";
                labMemberOf.Text = "<unjoined>";
                break;
            case JoinStatus.Workgroup:
                labDomainWorkgroup.Text = "Workgroup";
                labMemberOf.Text = NetworkInfo.Instance.Domain;
                break;
            case JoinStatus.Domain:
                labDomainWorkgroup.Text = "Domain";
                labMemberOf.Text = NetworkInfo.Instance.Domain;
                break;            
        }

        lvRoles.DataSource = NetworkGeneral.Roles;
        lvRoles.DataBind();
        IPGlobal = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties();        
    }
    protected void lbTcpConnections_Click(object sender, EventArgs e)
    {
        labWhat.Text = "Active TCP Connections:";

        panConnections.Visible = true;
        panListeners.Visible = false;
        lvConnections.DataSource = IPGlobal.GetActiveTcpConnections().OrderBy(i => i.State);
        lvConnections.DataBind();        
    }
    protected void lbTcpListeners_Click(object sender, EventArgs e)
    {
        labWhat.Text = "Active TCP Listeners";

        panListeners.Visible = true;
        panConnections.Visible = false;
        lvListerners.DataSource = IPGlobal.GetActiveTcpListeners().OrderBy(i => i.Address.ToString()).ThenBy(i => i.Port);
        lvListerners.DataBind();
    }
    protected void lbUdpListeners_Click(object sender, EventArgs e)
    {
        labWhat.Text = "Active UDP Listeners";

        panListeners.Visible = true;
        panConnections.Visible = false;
        lvListerners.DataSource = IPGlobal.GetActiveUdpListeners().OrderBy(i => i.Address.ToString()).ThenBy(i => i.Port);
        lvListerners.DataBind();
    }
}
